import React, { PureComponent } from 'react';

export default class ClassCounterTitleChange extends PureComponent {
    constructor(props) {
        super(props);

        this.state = {
            counter: 0
        }
    }

    componentDidMount() {

        // 修改DOM
        document.title = this.state.counter;

        // 订阅事件
        console.log('订阅事件');
    }

    // 每一次render就会执行这个生命周期函数
    componentDidUpdate() {
        document.title = this.state.counter;
        console.log('取消订阅');
    }


    render() {
        return (
            <div>
                <h2>当前计数: {this.state.counter}</h2>
                <button onClick={e => this.changeCounter()}>+1</button>
            </div>
        )
    }

    changeCounter() {
        this.setState({
            counter: this.state.counter + 1
        })
    }
}